﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class LearningObject : System.Web.UI.Page
{
    ILE_Global ileGlobal = new ILE_Global();
    ILE_Config ileConfig = new ILE_Config();

    protected int UserID
    {
        get
        {
            return (int)ViewState["UserID"];
        }
        set
        {
            ViewState["UserID"] = value;
        }
    }

    protected int LearnObjID
    {
        get
        {
            return (int)ViewState["LearnObjID"];
        }
        set
        {
            ViewState["LearnObjID"] = value;
        }
    }

    protected int CourseID
    {
        get
        {
            return (int)ViewState["CourseID"];
        }
        set
        {
            ViewState["CourseID"] = value;
        }
    }

    protected int SectionNumber
    {
        get
        {
            return (int)ViewState["SectionNumber"];
        }
        set
        {
            ViewState["SectionNumber"] = value;
        }
    }

    protected int StandardID
    {
        get
        {
            return (int)ViewState["StandardID"];
        }
        set
        {
            ViewState["StandardID"] = value;
        }
    }

    static DateTime viewDateTime = DateTime.Now;

    ClassBLL classLogic = new ClassBLL();
    //ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();
    LearnObjsBLL learnobjsLogic = new LearnObjsBLL();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            try
            {
                CourseID = Convert.ToInt32(Request.QueryString.Get("CourseID"));
                SectionNumber = Convert.ToInt32(Request.QueryString.Get("SectionNumber"));
                StandardID = Convert.ToInt32(Request.QueryString.Get("StandardID"));
                LearnObjID = Convert.ToInt32(Request.QueryString.Get("LearnObjID"));
            }
            catch
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID, SectionNumber, or StandardID<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
            }

            if (CourseID == 0 || SectionNumber == 0 || StandardID == 0 || LearnObjID == 0)
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid Access. This Course or Standard has been disabled.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                return;
            }

            try
            {
                UsersBLL usersLogic = new UsersBLL();
                UserID = usersLogic.GetUserByUsername(HttpContext.Current.User.Identity.Name.ToString()).UserID;
            }
            catch (NullReferenceException ex)
            {
                ileGlobal.RecordError("Assignments_Quiz.aspx.cs:Page_Load:001", ex, "User Session Ended Unexpectedly", 3);
                FormsAuthentication.SignOut();
                // *SteveMsg
                string _msg = "Your Session Information has ended. You have been logged out to prevent errors.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", _msg, 2, "Default.aspx");
                return;
            }

            viewDateTime = DateTime.Now;

            LoadCourseInfo();

            lblStandard.Text = ileGlobal.GetStandardNameByStandardID(StandardID);
            btnGoBackToStandardPage.PostBackUrl= lnkGoBackToStandardPage.NavigateUrl = "Standard.aspx?CourseID=" + CourseID.ToString() +
                                                                "&SectionNumber=" + SectionNumber.ToString() +
                                                                "&StandardID=" + StandardID.ToString();

            LoadLearningObject();
        }

        Page.Title = learnobjsLogic.GetLearnObj(LearnObjID).Title + " - ILE";
    }

    protected void LoadLearningObject()
    {
        ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();

        string LearnObjTitle = String.Empty;
        string LearnObjInstructions = String.Empty;

        LearnObjs.LearnObjsRow learnobj = learnobjsLogic.GetLearnObj(LearnObjID);

        ClassStandards.Courses_LearnObj_SectionRow sectionLearnObj = classStandardsLogic.GetClassSectionLearnObj(CourseID, SectionNumber, StandardID, LearnObjID);

        if (sectionLearnObj == null)
        {
            ClassStandards.Courses_LearnObjRow classLearnObj = classStandardsLogic.GetClassLearnObj(CourseID, StandardID, LearnObjID);
            if (classLearnObj.UseAlterInfo)
            {
                LearnObjTitle = classLearnObj.AlterTitle;
                LearnObjInstructions = classLearnObj.AlterDescription;
            }
            else
            {
                LearnObjTitle = learnobj.Title;
                LearnObjInstructions = learnobj.Instruction;
            }
        }
        else
        {
            if (sectionLearnObj.UseAlterInfo)
            {
                LearnObjTitle = sectionLearnObj.AlterTitle;
                LearnObjInstructions = sectionLearnObj.AlterDescription;
            }
            else
            {
                LearnObjTitle = learnobj.Title;
                LearnObjInstructions = learnobj.Instruction;
            }
        }

        lblLearnObjTitle.Text = LearnObjTitle;
        lblLearnObjInstructions.Text = LearnObjInstructions;
        if (learnobj.Link == "INTERNAL")
        {
            btnLearnObjStart.Visible = false;
            StatisticsBLL statsLogic = new StatisticsBLL();
            CourseID = Convert.ToInt32(Request.QueryString.Get("CourseID"));
            SectionNumber = Convert.ToInt32(Request.QueryString.Get("SectionNumber"));
            StandardID = Convert.ToInt32(Request.QueryString.Get("StandardID"));
            LearnObjID = Convert.ToInt32(Request.QueryString.Get("LearnObjID"));
            statsLogic.InsertLearnObjStats(LearnObjID, UserID, CourseID, SectionNumber, StandardID, viewDateTime);
        }
        else
        {
            btnLearnObjStart.OnClientClick = "window.open('" + learnobj.Link + "')";
        }
        
    }

    private void LoadCourseInfo()
    {
        Classes.CoursesRow course = classLogic.GetCourse(CourseID);
        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

        if (section.UseSectionInfo != true)
        {
            // USE CLASS DEFAULT INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + course.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
        }
        else
        {
            // USE SECTION INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + section.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
        }
    }

    protected void btnLearnObjStart_Click(object sender, EventArgs e)
    {
        StatisticsBLL statsLogic = new StatisticsBLL();
        statsLogic.InsertLearnObjStats(LearnObjID, UserID, CourseID, SectionNumber, StandardID, viewDateTime);
    }
}
